package com.ms.game.rebate.db;

import java.math.BigDecimal;

import com.ms.game.bank.BankMoneyChangeWay;
import com.ms.game.bank.db.BankDAO;
import com.ms.game.bank.db.BankLogModel;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.rebate.RebateCoinData;
import com.ms.game.rebate.RebateModifyStatus;
import com.ms.game.rebate.RebateType;
import com.ms.game.rebate.message.SGetRebateCoinResponse;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.analysisgroup.AnalysisGroupProcessor;
import com.ms.service.module.analysisgroup.CoinExpenditureWay;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;

public class GetSingleRebateCoinDCLAsync extends CentralAsyncCommand {

	private static final long serialVersionUID = 780335203913080199L;

	public GUID playerID;

	public String playerName;

	public String downlineId;
	
	public GUID accountID;

	public GUID sessionID;

	public int messageType;

	public long commisonTime;

	/**
	 * 如果是包月要從外面帶進來
	 */
	public RebateCoinData rebateCoinData;

	public BankMoneyChangeWay changeWay;
	
	public RebateType rebateType;
	/**
	 * 改變後銀行的錢
	 */
	private BigDecimal afterBankGold;
	
	
	/**
	 * 拿到的回饋金金額
	 */
	private BigDecimal gold = BigDecimal.ZERO;

	/**
	 * 是否有包月獎勵或是指定日期的抽佣金
	 */
	private boolean hasData = false;

	/**
	 * 領取時間
	 */
	public long nowTime;
	
	/**
	 * 包費類型
	 */
	public int bankBuyFeeType;

	RebateModel rebateModel;
	
	/**
	 * 送給統計用的C幣支出方式
	 */
	CoinExpenditureWay coinExpenditureWay = null;
	
	@Override
	public void asyncExecute() throws Exception {

		switch (changeWay) {
		case ADD_INVITE_FRIEND_COMMISSION:
			rebateModel = RebateDAO.getCommisonRebateCoinMarkReceived(playerID, nowTime);
		    if(hasData = (rebateModel != null)){
		    	gold = rebateModel.getTotalRebateCoin();
				afterBankGold = BankDAO.UpdateBankMoney(playerID, gold);
		    }
			
			break;
		case ADD_MONTHLY_FEE_DAILY_PAY:
			// 包月獎勵rebateCoinData 由外面帶進來0
			if(hasData = (rebateCoinData !=null)){
				gold = rebateCoinData.getRebateCoins();
				nowTime = rebateCoinData.getBelongsDate();
				afterBankGold = BankDAO.UpdateBankMoney(playerID, gold);
				BankDAO.UpdatePlayerMonthlyFee(playerID, nowTime, rebateCoinData.getMonthlyFeePayStage());
			}
			
			break;
		case ADD_WEEKLY_FEE_DAILY_PAY:
			// 包月獎勵rebateCoinData 由外面帶進來0
			if(hasData = (rebateCoinData !=null)){
				gold = rebateCoinData.getRebateCoins();
				nowTime = rebateCoinData.getBelongsDate();
				afterBankGold = BankDAO.UpdateBankMoney(playerID, gold);
				BankDAO.UpdatePlayerWeeklyFee(playerID, nowTime, rebateCoinData.getWeeklyFeePayStage(), RebateType.WEEKLY_FEE_DAILY_PAY);
			}
			
			break;
		case ADD_UPLINE_INVITE_REWARD:
			//上線邀請回饋獎勵
			rebateModel = RebateDAO.getUplineInviteRewardMarkReceived(playerID, downlineId);
			if (hasData = (rebateModel != null)) {
				gold = rebateModel.getTotalRebateCoin();
				afterBankGold = BankDAO.UpdateBankMoney(playerID, gold);
				
				//從model決定這是哪一種rebateType
				int rebateTypeId = rebateModel.getType();
				for(RebateType rebateType : RebateType.values()){
					if(rebateType.getId() == rebateTypeId){
						this.rebateType = rebateType;
						break;
					}
				}
			}
			break;
		}
		
		if(!hasData)
			return;
		
		String orderId = GUIDFactory.createUUID(GUIDType.TYPE_BANK_MONEY).toString();
		// 寫銀行Log
		BankLogModel logModel = new BankLogModel();
		logModel.setOrderId(orderId);
		logModel.setAccountId(accountID.toString());
		logModel.setPlayerId(playerID.toString());
		logModel.setPlayerName(playerName);
		logModel.setChangeWay(changeWay.getId());
		logModel.setChangeWayDesc(changeWay.getDesc());
		if (changeWay == BankMoneyChangeWay.ADD_UPLINE_INVITE_REWARD) {
			 logModel.setDownlineName(rebateModel.getDownlineName());
		}else if(changeWay == BankMoneyChangeWay.ADD_INVITE_FRIEND_COMMISSION){
			logModel.setRebateDate(rebateModel.getBelongsDate());
		}
		logModel.setBankGoldChangeValue(gold);
		logModel.setBankGoldBeforeChange(Helper.sub(afterBankGold, gold));
		logModel.setBankGoldAfterChange(afterBankGold);
		getCurrentModuleServer().saveDbLog(logModel);
		

		//回饋金log
		RebateLogModel rebateLogModel = new RebateLogModel();
		// orderId要跟銀行log的一樣
		rebateLogModel.setOrderId(orderId);
		rebateLogModel.setStatus(RebateModifyStatus.HAVE_RECEIVED);
		rebateLogModel.setTotalRebateCoin(gold);
		rebateLogModel.setPlayerId(playerID.toString());
		rebateLogModel.setPlayerName(playerName);
		rebateLogModel.setType(rebateType.getId());
		rebateLogModel.setTypeDesc(rebateType.getDesc());
		
		//Belongs date
		if(changeWay == BankMoneyChangeWay.ADD_MONTHLY_FEE_DAILY_PAY ){
			rebateLogModel.setBelongsDate(rebateLogModel.getEventDate());
		}else if(changeWay == BankMoneyChangeWay.ADD_WEEKLY_FEE_DAILY_PAY ){
			rebateLogModel.setBelongsDate(rebateLogModel.getEventDate());
		}else{
			rebateLogModel.setBelongsDate(rebateModel.getBelongsDate());
		}


		if(changeWay == BankMoneyChangeWay.ADD_UPLINE_INVITE_REWARD){
			rebateLogModel.setDownlineName(rebateModel.getDownlineName());
			rebateLogModel.setDownlineId(rebateModel.getDownlineId());
		}

		getCurrentModuleServer().saveDbLog(rebateLogModel);

	}
	

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		GameLobbyModule module = (GameLobbyModule) userData[0];
		SGetRebateCoinResponse response = new SGetRebateCoinResponse();

		int messageTye = 0;
		switch (changeWay) {
		case ADD_INVITE_FRIEND_COMMISSION:
			messageTye = MessageTypes.S_GET_COMMISON_REBATE_RESPONSE;
			coinExpenditureWay = CoinExpenditureWay.INVITE_CODE_COMMISSION;
			break;
		case ADD_MONTHLY_FEE_DAILY_PAY:
			messageTye = MessageTypes.S_GET_MONTHLY_DAILY_AWARD_REBATE_RESPONSE;
			coinExpenditureWay = CoinExpenditureWay.MONTH_DAILY_REWARD;
			break;
		case ADD_WEEKLY_FEE_DAILY_PAY:
			messageTye = MessageTypes.S_GET_WEEKLY_DAILY_AWARD_REBATE_RESPONSE;
			coinExpenditureWay = CoinExpenditureWay.WEEK_DAILY_REWARD;
			break;
		case ADD_UPLINE_INVITE_REWARD:
			messageTye = MessageTypes.S_GET_UPLINE_INVITE_REWARD_RESPONSE;
			
			if(rebateType != null){
				if(rebateType.equals(RebateType.UPLINE_INVITE_AWARD)){
					coinExpenditureWay = CoinExpenditureWay.INVITE_CODE_UPLINE_REWARD;
				}
				else if(rebateType.equals(RebateType.UPLINE_INVITE_AWARD_DISTRIBUTOR)){
					coinExpenditureWay = CoinExpenditureWay.INVITE_CODE_UPLINE_REWARD_DISTRIBUTOR;
				}
			}

			break;
		}

		if (hasData) {
			// 寫回zone上Player
			Player playerData = module.getZone().getPlayer(playerID);
			playerData.setBankGoldCoin(afterBankGold);

			if (changeWay == BankMoneyChangeWay.ADD_MONTHLY_FEE_DAILY_PAY) {
				playerData.setMonthlyAwardTime(nowTime);
				playerData.setMonthlyFeePayStage(rebateCoinData.getMonthlyFeePayStage());
			} else if (changeWay == BankMoneyChangeWay.ADD_WEEKLY_FEE_DAILY_PAY) {
				playerData.setWeeklyAwardTime(nowTime);
				playerData.setWeeklyFeePayStage(rebateCoinData.getWeeklyFeePayStage());
			}

			// 存成功
			response.errorCode = ErrorCode.SUCCESS;
			response.bankGold = afterBankGold.toString(); // 銀行金幣
		} else {
			response.errorCode = ErrorCode.REBATE_COIN_DATA_ERROR;
		}
		MessageAction.sendMessage(sessionID, messageTye, response);
		

		if(response.errorCode == ErrorCode.SUCCESS){
			//送到統計C幣支出
			AnalysisGroupProcessor.sendCoinExpenditureToAnalysis(coinExpenditureWay, gold);
			
		}
	}
}
